﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;


namespace gmeow.UI
{
    public partial class Accounts : Form
    {
        public Accounts(AccountRecord [] records)
        {
            InitializeComponent();
            accountCombo.Items.Clear();
            if (records == null)
                return;

            accountCombo.Items.AddRange(records);
            if(accountCombo.Items.Count > 0)
            {
                accountCombo.SelectedIndex = 0;
                LoadSelectedAccount();
            }
        }
        private void LoadSelectedAccount()
        {
            if (accountCombo.SelectedItem == null) return;

            var item = (AccountRecord) accountCombo.SelectedItem;
            accountBox.Text = item.AccountName;
            userBox.Text = item.Username;
            passBox.Text = item.Password;
        }

        private void accountCombo_SelectedValueChanged(object sender, EventArgs e)
        {
            LoadSelectedAccount();
        }

        private void accountBox_Leave(object sender, EventArgs e)
        {
            UpdateOrSaveCurrentAccount(SaveAccount.All);
        }

        private void UpdateOrSaveCurrentAccount(SaveAccount saveAccount)
        {
            if (string.IsNullOrEmpty(accountBox.Text))
                return;

            AccountRecord account = lookupAccount(accountBox.Text);
            if (account == null)
            {
                account = new AccountRecord();
                account.AccountName = accountBox.Text;
                accountCombo.Items.Add(account);
                accountCombo.SelectedItem = account;
                clearDetail();
            }

            if ((saveAccount == SaveAccount.All || saveAccount == SaveAccount.User) && !string.IsNullOrEmpty(userBox.Text))
                account.Username = userBox.Text;

            if ((saveAccount == SaveAccount.All || saveAccount == SaveAccount.Password) && !string.IsNullOrEmpty(passBox.Text))
                account.Password = passBox.Text;
        }

        private AccountRecord lookupAccount(string text)
        {
            foreach(object o in accountCombo.Items)
            {
                AccountRecord acc = (AccountRecord) o;
                if (acc.AccountName == text)
                    return acc;

            }
            return null;
        }

        private void userBox_TextChanged(object sender, EventArgs e)
        {
            UpdateOrSaveCurrentAccount(SaveAccount.User);
        }

        private void passBox_TextChanged(object sender, EventArgs e)
        {
            UpdateOrSaveCurrentAccount(SaveAccount.Password);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(accountCombo.SelectedIndex != -1)
                accountCombo.Items.Remove(accountCombo.SelectedItem);
            clearFields();
        }

        private void clearFields()
        {
            accountBox.Text = "";
            clearDetail();
        }

        private void clearDetail()
        {
            userBox.Text = "";
            passBox.Text = "";
        }

        public AccountRecord [] AccountRecords { get
        {
            List<AccountRecord> l = new List<AccountRecord>();
            foreach(object o in accountCombo.Items)
            {
                l.Add((AccountRecord)o);
            }
            return l.ToArray();
        }}

      
    }
    enum SaveAccount
    {
        All,
        User,
        Password
    }
    [Serializable]
    public class AccountRecord
    {
        public override string ToString() {
            return AccountName; }
        public string AccountName {get;set;}
        public string Username { get; set; }
        public string Password { get; set; }
    }
}